<script type="text/javascript">
    const {remote, clipboard} = require('electron');
    const {app} = remote;
    const projectRoot = remote.getGlobal('projectPath');
    const srcPath = remote.getGlobal('srcPath');
    // const ipc = require(`${srcPath}/mainlib/ipc`);
    const path = require('path');
    const core = require(`${srcPath}/mainlib/core`);
    const Type = require(`${srcPath}/mainlib/type`);
    const fs = require('fs');
    const fswin = require('fswin');
    const wh = require(`${srcPath}/mainlib/windows-helper`);
    
    const cm = {
        /** @var 搜索项目的索引 */
        indexes:{},
        
        /** @var 目录信息 */
        directories:{},
        
        /** @var 控制信息 */
        ctrl: {},
        
        /** @var 上次搜索的数据 */
        lastScaningData:{},

        /**
         * 获取还没有扫描的目录
         * @param dirList
         * @returns {*}
         */
        getNotScanDirs(dirList) {
            let newList = [];
            
            dirList.forEach(path => {
                if(!this.directories[path]) {
                    newList.push(path);
                }
            });
            
            return newList.length == 0 ? null : newList;
        },
        
        async scan(dirPath, depth, finish) {
            if(!fs.existsSync(dirPath)) {
                finish && finish();
                return ;
            }

            let stack  = 0;

            depth = Type.isUndefined(depth) ? -1 : depth;

            let _scan = (dirPath, currentDepth, callback) => {
                if(depth >=0 && currentDepth > depth) {
                    return;
                }

                stack ++;

                fs.readdir(dirPath, (e, files) => {
                    stack --;

                    if(e) {
                        console.log(e);
                        if(stack == 0) {
                            finish && finish();
                        }

                        return;
                    }

                    files.forEach(a => {
                        let file = path.join(dirPath, a);
                        callback(file);

                        try {
                            if(fs.statSync(file).isDirectory()) {
                                _scan(file, currentDepth+1, callback);
                            }
                        } catch(e) {
                            console.error(e);
                        }
                    });

                    if(stack == 0) {
                        finish && finish();
                    }
                })
            };

            _scan(dirPath, 0, callback);
        },

        async search(keyword) {
            return new Promise((ok, fail) => {
                for(let i=0; i<999999999; i++) {
                    let a = 1;
                    this.scan();
                }
                ok();
            })
        },
        
        
    }

    core.on(core.ET.CM_SEARCH, (e, data) => {
        console.log(data);
    });

    core.trigger(core.ET.CM_SEARCH_RESULT, {list:1});
    
    async function main() {
        console.log('ok');
        await cm.search();
        console.log('1111111111111');
    }
    
    main();
</script>
Searching Thread